package messengerclient;

import java.util.ArrayList;
import java.util.List;

import messengerclient.Contact;

public class ContactList
{
	private static ContactList contactList;
	private static Object locker = new Object();
	private List<Contact> list;
	private ContactList()
	{
		list = new ArrayList<Contact>();
	}
	
	public static ContactList instance()
	{
		if (contactList == null)
		{
			synchronized(locker)
			{
				if (contactList == null)
					contactList = new ContactList();
			}
		}
		return contactList;
	}
	
	public void add(Contact contact)
	{
		synchronized(list)
		{
			if (list.indexOf(contact) == -1)
			{
				list.add(contact);
			}
		}
	}
	
	public void add(String host, int port)
	{
		Contact contact = new Contact(host, port);
		add(contact);
	}
	
	public Contact get(int index)
	{
		synchronized(list)
		{
			if (list.size() > index)
				return list.get(index);
			else
				return null;
		}
	}
	
	public int getIndex(Contact contact)
	{
		synchronized(list)
		{
			return list.indexOf(contact);
		}
	}
	public int getCount()
	{
		synchronized(list)
		{
			return list.size();
		}
	}
    
    public boolean contains(Contact contact)
    {
        synchronized(list)
        {
            return list.indexOf(contact) != -1;
        }
    }
}